package Gr

import (
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
	"strings"
)

func (cm ColumnMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (cm ColumnMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(cm, parameter, value)
}
func (cm ColumnMe) Comment(parameter parDef.Parameter) (result global.Return, err error) {
	var event = "[Comment]"
	var execSql []string
	execSql = append(execSql, fmt.Sprintf("select "+
		"COLUMN_NAME as columnName,"+
		"COLUMN_COMMENT as columnComment"+
		"from "+
		"information_Schema.columns "+
		"where "+
		"table_schema in ('%v') "+
		"and table_name in ('%v') order by ORDINAL_POSITION",
		parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.SMap, execSql); err != nil {
		err = ref.ErrAddPrintf("Comment", err)
		return
	}
	if result.Result == nil {
		return
	}
	var p = make(map[string]string)
	for _, vv := range result.Result.([]map[string]any) {
		pp := mapNullActive(vv)
		column, ok1 := pp["columnName"]
		comment, ok2 := pp["columnComment"]
		if ok1 && ok2 {
			p[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v", comment)
		}
	}
	result.Result = p
	return
}
func (cm ColumnMe) LobMeta(parameter parDef.Parameter) (result global.Return, err error) {
	var event = "[LobMeta]"
	var execSql []string
	execSql = append(execSql, fmt.Sprintf("SELECT "+
		"`COLUMN_NAME` as `columnName`, "+
		"`DATA_TYPE` as `columnType` "+
		"FROM "+
		"INFORMATION_SCHEMA.COLUMNS "+
		"WHERE "+
		"TABLE_SCHEMA = '%v'  AND "+
		"TABLE_NAME = '%v' AND "+
		"DATA_TYPE IN ('tinyblob', 'blob', 'mediumblob', 'longblob')",
		parameter.Object.Schema,
		parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.SMap, execSql); err != nil {
		err = ref.ErrAddPrintf("LobMeta", err)
		return
	}
	if result.Result == nil {
		return
	}
	var p = make(map[string]string)
	for _, vv := range result.Result.([]map[string]any) {
		pp := mapNullActive(vv)
		column, ok1 := pp["columnName"]
		colType, ok2 := pp["columnType"]
		if ok1 && ok2 {
			p[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v", colType)
		}
	}
	result.Result = p
	return
}
func (cm ColumnMe) BigVarcharMeta(parameter parDef.Parameter) (result global.Return, err error) {
	var event = "[BigVarcharMeta]"
	var execSql []string
	execSql = append(execSql, fmt.Sprintf("SELECT "+
		"`COLUMN_NAME` as `columnName`, "+
		"`DATA_TYPE` as `columnType`,"+
		"`CHARACTER_MAXIMUM_LENGTH` as `dataLength` "+
		"FROM "+
		"INFORMATION_SCHEMA.COLUMNS "+
		"WHERE "+
		"TABLE_SCHEMA = '%v' AND "+
		"TABLE_NAME = '%v' AND "+
		"DATA_TYPE in ('varchar','text','mediumtext','longtext')  AND "+
		"CHARACTER_MAXIMUM_LENGTH > %d",
		parameter.Object.Schema,
		parameter.Object.Table,
		1000))
	if result, err = forExecQuerySql(event, parameter, ETL.SMap, execSql); err != nil {
		err = ref.ErrAddPrintf("BigVarcharMeta", err)
		return
	}
	if result.Result == nil {
		return
	}
	var p = make(map[string]string)
	for _, vv := range result.Result.([]map[string]any) {
		pp := mapNullActive(vv)
		column, ok1 := pp["columnName"]
		colType, ok2 := pp["columnType"]
		colLength, ok3 := pp["dataLength"]
		if ok1 && ok2 && ok3 {
			p[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v(%v)", colType, colLength)
		}
	}
	result.Result = p
	return
}

func (cm ColumnMe) MapConversionMeta() {

}

func (cm ColumnMe) Meta(parameter parDef.Parameter) (result global.Return, err error) {
	var event = "[Meta]"
	var res []columnMeta
	var execSql []string
	execSql = append(execSql, fmt.Sprintf("select "+
		"table_Schema as databaseName,"+
		"table_name as tableName,"+
		"COLUMN_NAME as columnName,"+
		"DATA_TYPE as columnType,"+
		"COLUMN_TYPE as dataType,"+
		"NUMERIC_PRECISION as dataPrecision,"+
		"NUMERIC_SCALE as dataScale,"+
		"ORDINAL_POSITION as columnSeq,"+
		"IS_NULLABLE as isNull,"+
		"CHARACTER_SET_NAME as charset,"+
		"COLLATION_NAME as collationName,"+
		"COLUMN_COMMENT as columnComment,"+
		"REPLACE(REPLACE(COLUMN_DEFAULT, 'CHARACTER SET utf8mb4', ''), 'CHARACTER SET utf8', '') as columnDefault,"+
		"EXTRA as autoIncrement  "+
		"from "+
		"information_Schema.columns "+
		"where "+
		"table_schema in ('%v') "+
		"and table_name in ('%v') order by ORDINAL_POSITION",
		parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.SMap, execSql); err != nil {
		err = ref.ErrAddPrintf("Meta", err)
		return
	}
	if result.Result == nil {
		return
	}
	for _, v := range result.Result.([]map[string]interface{}) {
		var vv = mapNullActive(v)
		if p, ok := vv["autoIncrement"]; ok {
			switch strings.ToLower(fmt.Sprintf("%v", p)) {
			case "auto_increment":
				vv["autoIncrement"] = true
			default:
				vv["autoIncrement"] = false
			}
		}
		var dd = columnMeta{}
		if err = mapStringToStruct(vv, &dd); err != nil {
			err = ref.ErrAddPrintf("Meta", err)
			return
		}
		res = append(res, dd)
	}
	result.InsideResult = res
	return
}
